package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.dto.serverInfo.ServerInfoDTO;
import com.etone.smartAudit.enums.IapDataField;
import com.etone.smartAudit.service.ServerCollectService;
import com.etone.smartAudit.service.ServerDiskService;
import com.etone.smartAudit.service.ServerInfoService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.serverInfo.ServerInfoBaseVO;
import com.etone.smartAudit.vo.serverInfo.ServerInfoDetailVO;
import com.etone.smartAudit.vo.serverInfo.ServerInfoListVO;
import com.etone.smartAudit.vo.serverInfo.ServerInfoTotalVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.CpuUsedRateVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.DiskUsedVO;
import com.etone.smartAudit.vo.serverInfo.serverCollect.MemoryUsedVO;
import com.etone.smartAudit.vo.serverInfo.serverDisk.ServerDiskVO;
import com.etone.smartAudit.vo.serverPath.ServerPathVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-06-16 19:29:41
 * @Description ServerController
 */
@AllArgsConstructor
@RestController
@Api(tags = "服务器", value = "ServerController")
public class ServerInfoController {

    private ServerInfoService serverInfoService;

    private ServerCollectService serverCollectService;

    private ServerDiskService serverDiskService;

    @ApiOperation(value = "服务器基本信息")
    @GetMapping("/serverInfos/simple")
    private List<ServerInfoBaseVO> listSimple(
            @LoginAdmin @ApiIgnore Admin admin,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId
    ) {

        return serverInfoService.listSimple(itemId);
    }

    @ApiOperation(value = "服务器信息")
    @GetMapping("/serverInfos")
    private PagerVO<ServerInfoListVO> list(
            @ApiParam(value = "名称") @RequestParam(required = false) String name,
            @ApiParam(value = "ip") @RequestParam(required = false) String ip,
            @ApiParam(value = "服务器id") @RequestParam(required = false) Integer id,
            @ApiParam(value = "所属域") @RequestParam(required = false) IapDataField field,
            @ApiParam(value = "管理员") @RequestParam(required = false) String manager,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize) {
        return serverInfoService.list(name, ip, id, field, manager, page, pageSize);
    }

    @ApiOperation(value = "服务器信息汇总")
    @GetMapping("/serverInfo/analysis/total")
    private ServerInfoTotalVO serverInfoAnalysisTotal() {
        return serverInfoService.serverInfoAnalysisTotal();
    }

    @ApiOperation(value = "编辑服务器《完》")
    @PutMapping("/serverInfos/{id}")
    public void edit(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "服务器详情") @Validated @RequestBody ServerInfoDTO serverInfoDTO
    ) {
        serverInfoService.save(id, serverInfoDTO);
    }

    @ApiOperation(value = "新增服务器《完》")
    @PostMapping("/serverInfos")
    public void add(
            @ApiParam(value = "服务器详情") @Validated @RequestBody ServerInfoDTO serverInfoDTO
    ) {
        serverInfoService.save(null, serverInfoDTO);
    }

    @ApiOperation(value = "服务器详情《完》")
    @GetMapping("/serverInfos/{id}")
    public ServerInfoDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return serverInfoService.detail(id);
    }

    @ApiOperation(value = "删除服务器《完》")
    @DeleteMapping("/serverInfos/{id}")
    public void delete(@ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        serverInfoService.delete(id);
    }

    @ApiOperation(value = "服务器盘符信息《完》")
    @GetMapping("/serverInfos/{id}/disks")
    public List<ServerDiskVO> serverDiskList(@ApiParam(value = "id", required = true) @PathVariable Integer id) {
        return serverDiskService.list(id);
    }

    @ApiOperation(value = "cpu使用率记录《完》")
    @GetMapping("/serverInfos/{id}/cpu/usedRate/record")
    public List<CpuUsedRateVO> cpuUsedRateRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Date endTime
    ) {
        return serverCollectService.cpuUsedRateRecord(id, startTime, endTime);
    }

    @ApiOperation(value = "内存使用记录《完》")
    @GetMapping("/serverInfos/{id}/memory/used/record")
    public List<MemoryUsedVO> memoryUsedRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Date endTime
    ) {
        return serverCollectService.memoryUsedRecord(id, startTime, endTime);
    }

    @ApiOperation(value = "磁盘使用记录《完》")
    @GetMapping("/serverInfos/{id}/disk/used/record")
    public List<DiskUsedVO> diskUsedRecord(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "开始时间") @RequestParam(required = false) Date startTime,
            @ApiParam(value = "结束时间") @RequestParam(required = false) Date endTime
    ) {
        return serverCollectService.diskUsedRecord(id, startTime, endTime);
    }

    @ApiOperation(value = "服务器项目列表《完》")
    @GetMapping("/serverInfos/{id}/items")
    public PagerVO<ServerPathVO> listServerItems(
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return serverCollectService.listServerItems(id, page, pageSize);
    }

    @ApiOperation(value = "服务器所有项目列表《完》")
    @GetMapping("/serverInfos/items")
    public List<ServerPathVO> listAllServerItem(
            @LoginAdmin @ApiIgnore Admin admin,
            @ApiParam(value = "true:查看所有服务器项目信息，false:只查看自己的项目信息") @RequestParam(required = false, defaultValue = "false") Boolean all
    ) {
        return serverCollectService.listAllServerItem(admin, all);
    }
}
